package main

import (
	"fmt"
	"reflect"
	"sync"
	"time"
)

var println = fmt.Println

func newTask() {
	for {
		println("this is a newTask!")
		time.Sleep(time.Second) //延时1s
	}
}

func swap(a, b *int) {
	println("&a:", a, "&b:", b)
	*a, *b = *b, *a
}

func test() string {
	var wg sync.WaitGroup
	wg.Add(3)
	var ret string = "test"
	defer func() {
		ret += " end"
		println("defer", ret)
	}()
	return ret
}

// Person struct
type Person struct {
	name   string
	age    uint8
	gender string
}

func (p *Person) show() {
	println("name:", p.name, "age:", p.age, "gender:", p.gender)
}

func main() {
	println("hello go")
	// const 只支持数字，字符串和布尔，及上述类型的表达式
	var num = [5]int{1, 2, 3, 4, 5}
	println(len(num))
	map1 := map[string]string{"name": "xiaobai"}
	println(map1)
	println("---------数组遍历----------")
	for i, v := range num {
		println(i, ":", v)
	}
	println("---------字典遍历-----------")
	for v, k := range map1 {
		println(v, ":", k)
	}
	var a, b int = 10, 20

	swap(&a, &b)
	println("a:", a, "b:", b)

	var nums = [3]int{1, 2, 3}
	var slice = nums[1:2]
	println(reflect.TypeOf(nums))
	println(slice)
	println(reflect.TypeOf(slice))
	println("caption:", cap(slice))
	println(test())

	var p = Person{"xiaobai", 5, "male"}
	p.show()
	go newTask()
	i := 0
	for {
		i++
		println("this is main goroutine")
		time.Sleep(time.Second)
		if i == 2 {
			break
		}
	}
	// 强制类型转换
	fmt.Println([]rune("小白"))
	for _, char := range []rune("ab") {
		fmt.Println(char)
	}

}
